package com.ygqh.baby.quartz;

import com.ygqh.baby.exception.YgHttpException;
import com.ygqh.baby.handler.DefaultPromotionTagHandler;
import com.ygqh.baby.model.PromotionTag;
import com.ygqh.baby.service.*;
import com.ygqh.baby.service.task.YgBalanceTaskService;
import com.ygqh.baby.service.task.YgProductBookTaskService;
import com.ygqh.baby.service.task.YgProductSecondTaskService;
import com.ygqh.baby.utils.ExceptionUtil;
import com.ygqh.baby.utils.MailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("quartzJobHandler")
public class QuartzJobHandler {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private YgBalanceTaskService ygBalanceTaskService;
    @Autowired
    private LogService logService;
    @Autowired
    private YgAgentService ygAgentService;
    @Autowired
    private YgSupplierReconciliationService ygSupplierReconciliationService;
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private YgWholesalerPrebillRecordService ygWholesalerPrebillRecordService;
    @Autowired
    private YgProductBookTaskService ygProductBookTaskService;
    @Autowired
    private DefaultPromotionTagHandler defaultPromotionTagHandler;
    @Autowired
    private YgUserBargainService ygUserBargainService;
    @Autowired
    private YgProductSecondTaskService ygProductSecondTaskService;

    public void doPreToBalance(Long orderId) {
        try {
            ygBalanceTaskService.doPreToBalance(orderId);
        } catch (Exception e) {
            logService.logRebate("预收入转可提现", orderId);
            throw e;
        }
    }

    public void cancelAgent(Integer agentId, String cancelBy) {
        try {
            /*ygAgentInviteRecordService.cancelAgent(Long.valueOf(agentId.toString()), cancelBy);*/
            ygAgentService.cancelAgent(Long.valueOf(agentId.toString()), cancelBy);
        } catch (Exception e) {
            logger.error("资格取消：" + agentId, e);
            throw e;
        }
    }

    public void addSupplierReconciliation(Integer supplierId) {
        try {
            ygSupplierReconciliationService.addSupplierReconciliation(Long.valueOf(supplierId.toString()));
        } catch (Exception e) {
            try {
                mailUtil.sendWithHtml(new String[]{"sunshuo@yiigoo.cn"}, "执行商家对账结算任务",
                        "[supplierId=" + supplierId.toString() + ", error=" + ExceptionUtil.getExceptionMsg(e));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            throw e;
        }
    }

    public void trustoneAddOrder(String orderId) throws YgHttpException {
//		trustoneApi.trustoneAddOrder(Long.parseLong(orderId));
    }

    public void tailPricePaidRemind(Integer bookId, String taskType) {
        YgProductBookTaskService.TaskType taskTypeEnum = YgProductBookTaskService.TaskType.valueOf(YgProductBookTaskService.TaskType.class, taskType);
        ygProductBookTaskService.tailPricePaidRemind(Long.valueOf(bookId.toString()), taskTypeEnum);
    }

    public void executeWholesalerPrebillJob(Integer wholesalerId) {
        try {
            ygWholesalerPrebillRecordService.executeWholesalerPrebillJob(Long.valueOf(wholesalerId));
        } catch (Exception e) {
            try {
                mailUtil.sendWithHtml(new String[]{"sunshuo@yiigoo.cn"}, "执行批发商订单预结算任务",
                        "[wholesalerId=" + wholesalerId.toString() + ", error=" + ExceptionUtil.getExceptionMsg(e));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            throw e;
        }
    }

    public void updatePromotionTag(PromotionTag promotionTag) {
        logger.info("更新商品促销标记" + promotionTag.getJobName());
        defaultPromotionTagHandler.updatePromotionTag(promotionTag.getPromotionId(), promotionTag.getPromotionType(), null, null, false);
    }

    public void sendBargainInvitation(Integer userBargainId) {
        logger.info("sendBargainInvitation userBargainId={}", userBargainId);
        ygUserBargainService.sendBargainInvitation(Long.valueOf(userBargainId));

    }

    public void updateBargainExpiredStatus(Integer userBargainId) {
        logger.info("updateBargainExpiredStatus userBargainId={}", userBargainId);
        ygUserBargainService.updateBargainExpiredStatus(Long.valueOf(userBargainId));

    }

    public void doProductSecondJob(Integer secondId, String taskType) {
        logger.info("doProductSecondJob secondId={}", secondId);
        ygProductSecondTaskService.doProductSecondJob(Long.valueOf(secondId), taskType);
    }

}
